package com.kingboy.kbg.commons.handler;


import com.kingboy.kbg.core.common.base.Result;
import com.kingboy.kbg.core.common.exception.BizException;
import com.kingboy.kbg.core.common.exception.core.ExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create By 郭金宝 on 2018/6/12.<br>
 */
@ControllerAdvice(value = {
        "com.kingboy.kbg.auth",
        "com.kingboy.kbg.cloud.gateway",
        "com.kingboy.kbg.uums.repository.impl",
        "com.kingboy.kbg.uums.rest",
        "com.kingboy.kbg.demand.repository.impl"
})
@ResponseBody  //返回结果为json
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    public Result baseExceptionHandler(BizException ex) {
        log.error("BizException:", ex);
        return new Result(ex.getCode(), null, ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result otherExceptionHandler(Exception ex) {
        log.error("Exception:", ex);
        return new Result(ExceptionCode.SYSTEM_BUSY.getCode(), null, ex.getMessage());
    }
}
